package ie.dit.reg08.manager;

import java.util.*;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Entity {

	String id;
	boolean target; 
	String imageFileName;
	Hashtable<String, String> htEntityAttributes;
	
	public Entity(Node n) {
		htEntityAttributes = new Hashtable<String, String>();
		
		if(n.hasAttributes()) {
			NamedNodeMap attributes = n.getAttributes();
		
			id = (attributes.getNamedItem("ID")).getNodeValue();
		
			if((attributes.getNamedItem("TYPE")).getNodeValue().equals("distractor")) {
				target=false;
			} else {
				target=true;
			}
			
			//Extract the image name from the xml data
			imageFileName = (attributes.getNamedItem("IMAGE")).getNodeValue();
			
		} else {
			System.err.println("furnitureentity n has no attributes!");
		}
		
		Node tmp;
		
		if(n.hasChildNodes()) {
			NodeList nl = n.getChildNodes();
			int num = nl.getLength();
			
			for(int i=0; i<num; i++ ) {
				tmp = nl.item(i);
				if(tmp.getNodeName().equals("ATTRIBUTE")) {
					NamedNodeMap attributes = tmp.getAttributes();
					
					/*System.out.println("-------");
					for(int j=0; j<attributes.getLength(); j++) {
						tmp2 = attributes.item(j);
						System.out.println("tmp2 name:" + tmp2.getNodeName() + ", type: "+ tmp2.getNodeType() + ", value: " + tmp2.getNodeValue());
					}*/

					//Each attribute node contains 
					//3 attributenodes NAME,TYPE,Value
					//and each attribute node name, type and value properties
					//e.g:
					// name:NAME, type: 2, value: colour
					// name:TYPE, type: 2, value: literal
					// name:VALUE, type: 2, value: grey
				
					htEntityAttributes.put(attributes.item(0).getNodeValue(), attributes.item(2).getNodeValue());
				    
				}
			}
		}	
		
	}
		
	public String getid() {
		return id;
	}
		
	public boolean isTarget() {
		return target;
	}
	
	public String getImageFileName(){
		return imageFileName;
	}
	/*
	 * if there is a key:value pair in arg 1 that does not match
	 * a key:value pair in the description return false
	 * 
	 * NOTE: we exclude the "other" attributes
	 */
	public boolean fulfills(Hashtable<String, String> htDescriptionAttributes) {
		Enumeration<String> e = htDescriptionAttributes.keys();
		String k, v, v2;
		while(e.hasMoreElements()) {
			k = e.nextElement();
			if (!k.equals("other")) {
				if (htEntityAttributes.containsKey(k)) {
					v = (String) htDescriptionAttributes.get(k);
					v2 = (String) htEntityAttributes.get(k);
					if (!(v.equals(v2))) {
						return false;
					}
				} else {
					System.err.println("Entity fulfils: entity does not have attribute: "+ k);
					System.exit(1);
				}
			} 
		}
		return true;
	}
	
	public Hashtable<String, String> getAttributes() {
		return htEntityAttributes;
	}
	
	public String getAttributeValue(String s) {
		return (String)htEntityAttributes.get(s);
	}
	
	public String toString() {
		
		String s = "id = " + id + ", target = " + target + ", image = " + imageFileName; 
	
		Enumeration<String> e = htEntityAttributes.keys();
		String k, v;
		while(e.hasMoreElements()) {
			k = e.nextElement();
			v = htEntityAttributes.get(k);
			s = s + ", " + k + " = " + v;
		}
		return s;
	}
	
	public String getXMLEntityDescription() {
		  String type;
		  if(target) {
			  type = "target";
		  } else {
			  type = "distractor";
		  }
		  
		  String s = "<ENTITY ID=\"" + id + "\"" + " IMAGE=\"" +  imageFileName + "\"" + " TYPE=\"" + type + "\">\n";
		  
		  s = s + "<ATTRIBUTE-SET>\n";		  

		 Enumeration<String> e = htEntityAttributes.keys();
		String k, v, t;
		/*
		 * TODO: update type dummy stub in entity getXMLEntityDescription!
		 */
		t = "TYPE_DUMMY_STUB";
		while(e.hasMoreElements()) {
				k = e.nextElement();
				v = htEntityAttributes.get(k);
				s = s + "<ATTRIBUTE NAME=\"" + k + "\" TYPE=\"" + type + "\" VALUE=\"" + v + "\"/>\n";					
		}
		  s = s + "</ATTRIBUTE-SET>\n";
		  
		  s = s + "</ENTITY>\n";
			 		
		return s;
	}
}
